package com.tf8.treasure.utils;

public class DataMask {
    /**
     * 数据打码
     * raw 需要加*的数据
     * front 前边显示真实数据的位数
     * back  后边边显示真实数据的位数
     */
    public static String mask(Integer front, Integer back, String raw) {
        if (raw == null) {
            return null;
        }
        if (front != null && front >= 0 && back != null && back >= 0) {
            if (raw.length() <= front + back) {
                return raw;
            }
            int i = raw.length() - (front + back);
            return raw.substring(0, front) + makeString(i) + raw.substring(raw.length() - back, raw.length());
        } else if (front != null && front >= 0) {
            if (raw.length() < front) {
                return raw;
            }
            int i = raw.length() - front;
            return raw.substring(0, front) + makeString(i);
        } else if (back != null && back >= 0) {
            if (raw.length() < back) {
                return raw;
            }
            int i = raw.length() - back;
            return makeString(i) + raw.substring(raw.length() - back, raw.length());
        } else {
            return makeString(raw.length());
        }
    }

    private static String makeString(int count) {
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < count; i++) {
            s.append("*");
        }
        return s.toString();
    }
}
